package demo1;

import jdk.jfr.Label;

import java.util.Arrays;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: liangyuanzhang
 * Date: 2025-3-12
 * Time: 12:09
 */
public class MyArrayList implements IList {
    public int[] array;
    public int useSize;

    public static final int DEFAULY_CAPCITY = 10;// 默认容量

    public MyArrayList(){
        this.array = new int[DEFAULY_CAPCITY];
    }

    @Override
    public void add(int data) {
        if (isFull()){
            grow();
        }
        this.array[this.useSize] = data;
        this.useSize++;
    }

    private  void grow(){
        this.array = Arrays.copyOf(this.array,
                2*this.array.length);
    }

    public boolean isFull(){
        return this.useSize == array.length;
    }

    private void checkPos(int pos) throws PosIllegal{
        if (pos < 0 || pos > useSize){
            throw new PosIllegal("Pos位置不合法!");
        }
    }
    @Override
    public void add(int pos, int data) {
        /*
        插入数据的位置 要有前驱
         */
        try {
            checkPos(pos);
            if (isFull()){
                grow();
            }
            // 挪动元素
            for (int i = useSize-1;i >= pos;i--){
                array[i+1] = array[i];
            }
            array[pos] = data;
            useSize++;
        }catch (PosIllegal e){
            System.out.println("插入元素Pos位置不合法!");
            e.printStackTrace();
        }
    }

    @Override
    public boolean contains(int toFind) {
        for (int i = 0; i < this.useSize; i++) {
             if (array[i] == toFind){
                 return true;
             }
        }
        return false;
    }

    @Override
    public int indexOf(int toFind) {
        for (int i = 0; i < this.useSize; i++) {
            if (array[i] == toFind){
                return i;
            }
        }
        return 0;
    }

    private void checkPos2(int pos) throws PosIllegal{
        if (pos < 0 || pos >= useSize){
            throw new PosIllegal("Pos位置不合法!");
        }
    }
    @Override
    public int get(int pos) {

        try {
            checkEmpty();
            checkPos2(pos);
            return array[pos];
        }catch (PosIllegal e){
            e.printStackTrace();
        }catch (EmptyExcepyion e){
            e.printStackTrace();
        }
        return -1;
    }

    private void checkEmpty(){
        if (isEmpty()){
            throw new EmptyExcepyion("顺序表为空");
        }
    }

    public boolean isEmpty(){
        return useSize == 0;
    }
    @Override
    //更新的意思
    public void set(int pos, int value) {
        try {
            checkEmpty();
            checkPos2(pos);
            array[pos] = value;
        }catch (PosIllegal e){
            e.printStackTrace();
        }catch (EmptyExcepyion e){
            e.printStackTrace();
        }
    }

    @Override
    public void remove(int toRemove) {
        try {
            checkEmpty();
             int pos = indexOf(toRemove);
             if (pos == -1){
                 return;
             }
            for (int i = pos; i < useSize-1; i++) {
                array[i] = array[i+1];// 注意不要越界
            }
            useSize--;
        }catch (EmptyExcepyion e){
            e.printStackTrace();
        }

    }

    @Override
    public int size() {
        return this.useSize;
    }

    @Override
    public void clear() {
        /*for (int i = 0; i < useSize; i++) {
            array[i] = null;
        }*/
        useSize = 0;
    }

    @Override
    public void display() { // 遍历
        for (int i = 0; i < this.useSize; i++) {
            System.out.print(array[i]+" ");
        }

        // 这里所有都是0
        /*for (int x : array) {
            System.out.print(x + " ");
        }*/
    }
}
